local assets =
{
    -- Asset("ANIM", "anim/panda_fisherman_supply_pack.zip"),
    -- Asset( "IMAGE", "images/inventoryimages/panda_fisherman_supply_pack.tex" ),  -- 背包贴图
    -- Asset( "ATLAS", "images/inventoryimages/panda_fisherman_supply_pack.xml" ),
}

local function OnHit(inst, owner, target)
    inst.___pet_target_old = inst.___pet_target
    inst.___pet_target = nil

    inst.AnimState:PlayAnimation("idle", true)
    -- inst.AnimState:PlayAnimation("win")
    -- inst.SoundEmitter:KillSound("active_loop")
    -- inst.SoundEmitter:PlaySound("summerevent/carnival_games/herding_station/chicks/win")

    inst.components.projectile:Stop()

    target:PushEvent("new_location")

    inst:DoTaskInTime(math.random(10),function()
        -- inst.SoundEmitter:PlaySound("summerevent/carnival_games/herding_station/chicks/LP", "active_loop")
        inst.AnimState:PlayAnimation("run_pre")
        inst.AnimState:PushAnimation("run_loop",true)
        inst.___pet_target = inst.___pet_target_old
        inst.components.projectile:Throw(inst.___pet_target,inst.___pet_target)  

    end)

end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()
    inst.entity:AddDynamicShadow()

    inst.DynamicShadow:SetSize(.8, 0.8)

    MakeInventoryPhysics(inst)
    RemovePhysicsColliders(inst)

    inst.Transform:SetSixFaced()

    inst.AnimState:SetBank("carnivalgame_herding_chick")
    inst.AnimState:SetBuild("carnivalgame_herding_chick")
    inst.AnimState:PlayAnimation("idle", true)
    -- inst.AnimState:PlayAnimation("run_pre")
    -- inst.AnimState:PushAnimation("run_loop",true)

    -- inst:AddTag("projectile")
    inst:AddTag("FX")
    inst:AddTag("INLIMBO")

    inst.entity:SetPristine()

    inst.SoundEmitter:PlaySound("summerevent/carnival_games/herding_station/chicks/LP", "active_loop") -- this will be on until it dies,

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(0)

    inst:AddComponent("projectile")
    inst.components.projectile:SetSpeed(5)     -- wilson 6
    inst.components.projectile:SetHoming(false)
    inst.components.projectile:SetHitDist(2)
    inst.components.projectile:SetOnHitFn(OnHit)
    -- inst.components.projectile:SetOnMissFn(inst.Remove)
    -- inst.components.projectile:SetOnThrownFn(OnThrown)
    ----------------------------------------------------------------------------------------------------
    inst:DoTaskInTime(0,function()
        if inst.Ready ~= true then
            inst:Remove()
        end
    end)

    inst:DoTaskInTime(0.5,function()
        inst.AnimState:PlayAnimation("run_pre")
        inst.AnimState:PushAnimation("run_loop",true)
        inst.components.projectile:Throw(inst.___pet_target,inst.___pet_target)        
    end)
    inst:DoPeriodicTask(3,function()
        if inst.___pet_target then
            inst.components.projectile:Throw(inst.___pet_target,inst.___pet_target)  
        end
    end)
    ----------------------------------------------------------------------------------------------------
    return inst
end

local function pet_target()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    -- inst.entity:AddAnimState()
    -- inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()


    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")


    inst:DoTaskInTime(0,function()
        if inst.Ready ~= true then
            inst:Remove()
        end
    end)

    inst:ListenForEvent("new_location",function()
        local tree_pt = Vector3(inst.__tree.Transform:GetWorldPosition())
        local new_pt = inst.npc_base_lib:GetSpawnPoint(tree_pt,math.random(10,30)) or tree_pt
        local pt = Vector3(inst.Transform:GetWorldPosition())
        
        for i = 1, 30, 1 do
            if inst.npc_base_lib:Distance_Points(pt,new_pt) < 5 then
                new_pt = inst.npc_base_lib:GetSpawnPoint(tree_pt,math.random(10,30)) or tree_pt
            else
                break
            end
        end

        inst.Transform:SetPosition(new_pt.x,0,new_pt.z)   
        
        

    end)



    return inst
end

return Prefab("npc_item_clockwork_egg_pet", fn,assets),
    Prefab("npc_item_clockwork_egg_pet_target", pet_target,assets)